
package view;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import loginwebservice.Branch;

/**
 * @version 1.0
 * @date 10/04/2012
 * @author john
 */
public class MainWindow extends Application {
    
    private static Task fileAnalysis;
    private String filePath="";
    private long fileLinesNumber=-1;
    private static Branch activeUser;
    
    private static Stage loginDialog;
    private static Stage contactDialog;
    private GridPane loadFilePane;
    private GridPane statsPane;
    private GridPane progressPane;
    private double defaultWidth=900;
    private double defaultHeight=600;
    
    private Group root;
    private Scene scene;
    private BorderPane borderPane;
    private static MenuBar menuBar;
    private static Menu fileM;
    private static Menu reportsM;
    private static Menu helpM;
    private static MenuItem loadMI;
    private static MenuItem loadStateMI;
    private static MenuItem fullscreenMI;
    private static MenuItem exitMI;
    private static MenuItem newReportMI;
    private static MenuItem openReportMI;
    private static MenuItem saveReportMI;
    private static MenuItem contentMI;
    private static MenuItem contactMI;
    private static MenuItem aboutMI;
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("SATAcredito");
        root=new Group();
        scene=new Scene(root, defaultWidth, defaultHeight, Color.WHITE);
        scene.getStylesheets().add("view/styles.css");
        initMenu(primaryStage);
        borderPane=new BorderPane();
        borderPane.setTop(menuBar);
        loginDialog=new LoginDialog(primaryStage);
        loginDialog.sizeToScene();
        contactDialog=new ContactDialog(primaryStage);
        contactDialog.sizeToScene();
        loadFilePane = new LoadFilePane(primaryStage,this);
        progressPane=new ProgressPane(scene, this, "", 0);
        root.getChildren().addAll(borderPane);
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(finishApplication());
        primaryStage.show();
        showLoginDialog();
    }
    
    /**
     * 
     * @param primaryStage 
     */
    private void initMenu(final Stage primaryStage){
        menuBar=new MenuBar();
        menuBar.prefWidthProperty().bind(scene.widthProperty());
        initFileMenu(primaryStage);
        initReportsMenu(primaryStage);
        initHelpMenu(primaryStage);
        menuBar.getMenus().setAll(fileM,reportsM,helpM);
    }
    
    /**
     * 
     * @param primaryStage 
     */
    private void initFileMenu(final Stage primaryStage){
        fileM=new Menu("Archivo");
        loadMI=new MenuItem("Cargar archivo");
        loadStateMI=new MenuItem("Ver estado de carga");
        fullscreenMI=new MenuItem("Pantalla Completa");
        exitMI=new MenuItem("Salir");
        loadMI.setOnAction(loadFile());
        loadStateMI.setOnAction(seeLoadState());
        fullscreenMI.setOnAction(fullScreen(primaryStage));
        exitMI.setOnAction(exit());
        loadMI.setAccelerator(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN));
        fullscreenMI.setAccelerator(new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN));
        exitMI.setAccelerator(new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN));
        fileM.getItems().setAll(
            loadMI,
            loadStateMI,
            new SeparatorMenuItem(),
            fullscreenMI,
            new SeparatorMenuItem(),
            exitMI
        );
    }
    
    /**
     * 
     * @param primaryStage 
     */
    private void initReportsMenu(final Stage primaryStage){
        reportsM=new Menu("Reportes");
        newReportMI=new MenuItem("Nuevo reporte");
        openReportMI=new MenuItem("Abrir reporte guardado");
        saveReportMI=new MenuItem("Guardar reporte");
        newReportMI.setOnAction(newReport(primaryStage));
        openReportMI.setOnAction(openReport());
        saveReportMI.setOnAction(saveReport());
        newReportMI.setAccelerator(new KeyCodeCombination(KeyCode.R, KeyCombination.CONTROL_DOWN));
        openReportMI.setAccelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN));
        saveReportMI.setAccelerator(new KeyCodeCombination(KeyCode.G, KeyCombination.CONTROL_DOWN));
        reportsM.getItems().setAll(newReportMI,openReportMI,saveReportMI);
    }
    
    /**
     * 
     * @param primaryStage 
     */
    private void initHelpMenu(final Stage primaryStage){
        helpM=new Menu("Ayuda");
        contentMI=new MenuItem("Contenido");
        contactMI=new MenuItem("Contacto");
        aboutMI=new MenuItem("Acerca");
        contentMI.setOnAction(showContent());
        contactMI.setOnAction(contactAction());
        aboutMI.setOnAction(aboutAction());
        helpM.getItems().setAll(contentMI,contactMI,new SeparatorMenuItem(),aboutMI);
    }
    
    /**
     * 
     * @param filePath
     * @param numberOfLinesFile 
     */
    protected void startFileAnalysis(String filePath, long numberOfLinesFile){
        this.filePath=filePath;
        this.fileLinesNumber=numberOfLinesFile;
        progressPane=new ProgressPane(scene, this, filePath, numberOfLinesFile);
        fileAnalysis=((ProgressPane)progressPane).createTask();
        new Thread(fileAnalysis).start();
        loadStateMI.fire();
    }
    
    /**
     * 
     */
    protected void endFileAnalysis(){
        this.filePath="";
        this.fileLinesNumber=-1;
        ((LoadFilePane)loadFilePane).enableLoad();
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> loadFile(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                borderPane.setCenter(loadFilePane);
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> seeLoadState(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                borderPane.setCenter(progressPane);
            }
        };
    }
    
    /**
     * 
     * @param primaryStage
     * @return 
     */
    private EventHandler<ActionEvent> fullScreen(final Stage primaryStage){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                primaryStage.setFullScreen(!primaryStage.isFullScreen());
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> exit(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                if(fileAnalysis!=null && fileAnalysis.isRunning())
                    fileAnalysis.cancel(true);
                Platform.exit();
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> newReport(final Stage stage){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                statsPane=new StatsPane(stage);
                borderPane.setCenter(statsPane);
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> openReport(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                // Mostrar GUI para abrir reporte
                borderPane.setCenter(new Label("Mostrar GUI para abrir reporte"));
                System.err.println("openReport EventHandler not implemented");
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> saveReport(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                // Mostrar GUI para guardar reporte
                borderPane.setCenter(new Label("Mostrar GUI para guardar reporte"));
                System.err.println("saveReport EventHandler not implemented");
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> showContent(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                // Mostrar GUI con contenido de ayuda
                // o ejecutar navegador para ayuda en linea
                borderPane.setCenter(new Label("Mostrar GUI con contenido de ayuda"));
                System.err.println("showContent EventHandler not implemented");
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> contactAction(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                // Mostrar GUI con formulario para contacto
                borderPane.setCenter(new Label("Mostrar GUI con formulario para contacto"));
                System.err.println("contactAction EventHandler not implemented");
            }
        };
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<ActionEvent> aboutAction(){
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                // Mostrar GUI de informacion de equipo de desarrollo
                borderPane.setCenter(new Label("Mostrar GUI de informacion de equipo de desarrollo"));
                System.err.println("aboutAction EventHandler not implemented");
            }
        };
    }
    
    /**
     * 
     * @param newState 
     */
    protected static void changeMenuItemsState(boolean newState){
        loadMI.setDisable(!newState);
        loadStateMI.setDisable(!newState);
    }

    public static Branch getActiveUser() {
        return activeUser;
    }

    public static void setActiveUser(Branch activeUser) {
        MainWindow.activeUser = activeUser;
    }
    
    /**
     * 
     * @return 
     */
    private EventHandler<WindowEvent> finishApplication(){
        return new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                event.consume();
                exitMI.fire();
            }
        };
    }
    
    /**
     * 
     */
    public static void showLoginDialog(){
        ((LoginDialog)loginDialog).showStage();
    }
    
    /**
     * 
     */
    public static void closeLoginDialog(){
        ((LoginDialog)loginDialog).closeDialog();
    }
    
    /**
     * 
     */
    public static void showContactDialog(){
        ((ContactDialog)contactDialog).showStage();
    }
    
    /**
     * 
     */
    public static void closeContactDialog(){
        contactDialog.close();
    }
    
    /**
     * 
     */
    public static void showLoadPane(){
        loadMI.fire();
    }
    
    public static void main(String[]args){
        launch(args);
    }
    
}